﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RuYiAdmin.Net.Common.Global;
using RuYiAdmin.Net.Common.Utility;
using RuYiAdmin.Net.Entity.Base;
using RuYiAdmin.Net.Entity.BusinessEnum;
using RuYiAdmin.Net.Entity.CoreEntity;
using RuYiAdmin.Net.Service.Base;
using RuYiAdmin.Net.WebApi.AppCode.ActionFilters;
using RuYiAdmin.Net.WebApi.AppCode.AuthorizationFilter;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

/// <summary>
/// 控制器基类定义
/// </summary>
namespace RuYiAdmin.Net.WebApi.AppCode.FrameworkBase
{
    /// <summary>
    /// 控制器基类
    /// </summary>
    [Authorize]
    [ActionAuthorization]
    [ApiController]
    [Route(GlobalContext.RouteTemplate)]
    public class BaseController<T> : ControllerBase where T : BaseEntity
    {
        /*声明：

        1.该基类方法可用于构建独立的WebApi。

        2.方法不够用时可调用基类服务、自行添加。

        3.按钮与视图的权限鉴别，请在具体的业务控制器中实现。

        */

        #region 属性与构造函数

        /// <summary>
        /// 服务层基类实例
        /// </summary>
        private readonly IBaseService<T> baseService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseService"></param>
        public BaseController(IBaseService<T> baseService)
        {
            this.baseService = baseService;
        }

        #endregion        

        #region 通用方法

        /// <summary>
        /// 分页查询列表
        /// </summary>
        /// <param name="queryCondition">查询条件</param>
        /// <returns>ActionResult</returns>
        [HttpPost]
        [Log(OperationType.QueryList)]
        public IActionResult GetPage(QueryCondition queryCondition)
        {
            var actionResult = this.baseService.GetPage(queryCondition);
            return Ok(actionResult);
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="queryCondition">查询条件</param>
        /// <returns>ActionResult</returns>
        [HttpPost]
        [Log(OperationType.QueryList)]
        public IActionResult GetList(QueryCondition queryCondition)
        {
            var actionResult = this.baseService.GetList(queryCondition);
            return Ok(actionResult);
        }

        /// <summary>
        /// 按编号查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [Log(OperationType.QueryEntity)]
        public IActionResult GetEntityById(Guid id)
        {
            var actionResult = this.baseService.GetById(id);
            return Ok(actionResult);
        }

        /// <summary>
        /// 新增对象
        /// </summary>
        /// <param name="t">对象</param>
        /// <returns>ActionResult</returns>
        [HttpPost]
        [Log(OperationType.AddEntity)]
        public IActionResult AddEntity([FromBody] T t)
        {
            var actionResult = this.baseService.Add(t);
            return Ok(actionResult);
        }

        /// <summary>
        /// 编辑对象
        /// </summary>
        /// <param name="t">对象</param>
        /// <returns>ActionResult</returns>
        [HttpPut]
        [Log(OperationType.EditEntity)]
        public IActionResult EditEntity([FromBody] T t)
        {
            var actionResult = this.baseService.Update(t);
            return Ok(actionResult);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>ActionResult</returns>
        [HttpDelete("{id}")]
        [Log(OperationType.DeleteEntity)]
        public IActionResult DeleteEntityById(Guid id)
        {
            var actionResult = this.baseService.Delete(id);
            return Ok(actionResult);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">编号数组</param>
        /// <returns>ActionResult</returns>
        [HttpDelete("{ids}")]
        [Log(OperationType.DeleteEntity)]
        public IActionResult DeleteEntitiesByIds(String ids)
        {
            var array = StringUtil.GetGuids(ids);
            var actionResult = this.baseService.DeleteRange(array);
            return Ok(actionResult);
        }

        /// <summary>
        /// 下载Excel
        /// </summary>
        /// <param name="excelId">文件编号</param>
        /// <returns>ActionResult</returns>
        [HttpGet("{excelId}")]
        [Log(OperationType.DownloadFile)]
        public IActionResult DownloadExcel(String excelId)
        {
            //存储路径
            var path = Path.Join(GlobalContext.DirectoryConfig.GetTempPath(), "/");

            //文件路径
            var filePath = Path.Join(path, excelId + ".xls");

            //文件读写流
            var stream = new FileStream(filePath, FileMode.Open);

            stream.Position = 0;

            return File(stream, "application/octet-stream", "错误信息.xls");
        }

        /// <summary>
        /// 下载Excel模板
        /// </summary>
        /// <param name="templateId">文件编号</param>
        /// <returns>ActionResult</returns>
        [HttpGet("{templateId}")]
        [Log(OperationType.DownloadFile)]
        public IActionResult DownloadTemplate(String templateId)
        {
            //存储路径
            var path = Path.Join(GlobalContext.DirectoryConfig.GetTemplateDirectory(), "/");

            //文件路径
            var filePath = Path.Join(path, templateId + ".xls");

            //文件读写流
            var stream = new FileStream(filePath, FileMode.Open);

            stream.Position = 0;

            return File(stream, "application/octet-stream", "导入模板.xls");
        }

        #endregion
    }
}
